/**
 *
 * Licensed Materials - Property of IBM 
 * 
 * OCO Source Materials 
 * 
 * (C) Copyright IBM Corp. 2011, 2012 All Rights Reserved 
 * 
 * The source code for this program is not published or other- 
 * wise divested of its trade secrets, irrespective of what has 
 * been deposited with the U.S. Copyright Office. 
 *
 */

/**
 * CHANGE HiSTORY:
 * Author             Time                 Description
 *
 */
package com.ibm.ba.demo.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.ibm.ba.demo.entity.Product;
import com.ibm.ba.demo.entity.SPSSModelConfiguration;

@Repository
public class ProductDao extends AbstractDao {
	private static Logger logger = Logger.getLogger(ProductDao.class);
	private static String SQL_GET_ALL_PRODUCTS = "SELECT P_PARTKEY, P_NAME FROM PARTCH WHERE P_PARTKEY IN (:ids)";
	private static String SQL_GET_PRODUCTS_BY_CUSTOMERID = "SELECT DISTINCT T1.L_PARTKEY FROM LINEITEM T1,ORDERS T2 WHERE T1.L_ORDERKEY = T2.O_ORDERKEY AND T2.O_CUSTKEY= :id";

	@Autowired
	private SPSSModelConfiguration modelConfig;

	public List<Product> getAllProducts() {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("ids",
				modelConfig.getIntegerList(modelConfig.getProductIds()));
		logger.info("product ids: "
				+ modelConfig.getIntegerList(modelConfig.getProductIds()));
		List<Product> list = getJdbcTemplate().query(SQL_GET_ALL_PRODUCTS,
				paramMap, new ProductRowMapper());
		return list;
	}

	public List<Product> getProductNames(List<Product> prodList) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		List<Integer> prodIds = new ArrayList<Integer>();
		for (Product product : prodList) {
			prodIds.add(product.getProductId());
		}
		paramMap.put("ids", prodIds);
		logger.info("product ids: "
				+ modelConfig.getIntegerList(modelConfig.getProductIds()));
		List<Product> list = getJdbcTemplate().query(SQL_GET_ALL_PRODUCTS,
				paramMap, new ProductRowMapper());
		for (Product product : prodList) {
			for (Product tmpProduct : list) {
				if (product.getProductId() == tmpProduct.getProductId()) {
					product.setProductName(tmpProduct.getProductName());
				}
			}
		}
		return prodList;
	}

	public Map<String, Object> getProductIdsByCustomerId(int customerId) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("id", customerId);
		logger.info("customer id: " + customerId);
		Map<String, Object> resultMap = getJdbcTemplate().query(
				SQL_GET_PRODUCTS_BY_CUSTOMERID, paramMap,
				new ProductsResultSetExtractor());
		resultMap.put(modelConfig.getCustField(), customerId);
		return resultMap;
	}

	public List<Map<String, Object>> getProductIdsByCustomerIds(
			List<Integer> customerIds) {
		ArrayList<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < customerIds.size(); i++) {
			resultList.add(getProductIdsByCustomerId(customerIds.get(i)));
		}
		return resultList;
	}

}
